home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UCRASM25.ARJ / ATOI.ASM < prev    next >
Assembly Source File  |  1991-10-12  |  2KB  |  117 lines

  1. StdGrp        group    stdlib,stddata
  2. stddata        segment    para public 'sldata'
  3. stddata        ends
  4. ;
  5. stdlib        segment    para public 'slcode'
  6.         assume    cs:stdgrp
  7. ;
  8. ;
  9. ; ATOI-    Converts the string pointed at by ES:DI to a signed integer value
  10. ;    and returns this integer in the AX register.
  11. ;
  12. ;    Returns with the carry flag clear if no error, set if overflow.
  13. ;
  14.         public    sl_atoi
  15. sl_atoi        proc    far
  16.         push    di
  17.         call    far ptr sl_atoi2
  18.         pop    di
  19.         ret
  20. sl_atoi        endp
  21. ;
  22.         public    sl_atoi2
  23. sl_atoi2    proc    far
  24.         push    cx
  25.         push    dx
  26.         xor    cx, cx
  27.         mov    ah, ch                ;Assume it's positive
  28.         cmp    byte ptr es:[di], '-'
  29.         jne    DoAtoI
  30. ;
  31. ; Set up for negative numbers.
  32. ;
  33.         inc    di                ;Skip "-"
  34.         mov    ah, 1                ;Flag negative value.
  35. ;
  36. DoAtoI:        call    NAtoI
  37.         jc    WasError            ;Quit if error.
  38.         cmp    ah, 0
  39.         je    IsPositive
  40.         neg    cx
  41.         clc
  42.         jmp    WasError            ;Not really an error.
  43. ;
  44. IsPositive:    or    cx, cx                ;See if overflow
  45.         clc
  46.         jns    WasError            ;Not an error
  47.                 stc                    ;Error if negative.
  48. WasError:    mov    ax, cx
  49.         pop    dx
  50.         pop    cx
  51.         ret
  52. sl_atoi2    endp
  53. ;
  54. ;
  55. ;
  56. ; ATOU-    Just like ATOI but this guy only does unsigned numbers.
  57. ;
  58.         public    sl_atou
  59. sl_atou        proc    far
  60.         push    di
  61.         call    far ptr sl_atou2
  62.         pop    di
  63.         ret
  64. sl_atou        endp
  65. ;
  66. ;
  67.         public    sl_atou2
  68. sl_atou2    proc    far
  69.         push    cx
  70.         push    dx
  71.         xor    cx, cx
  72.         call    NAtoI
  73.         mov    ax, cx
  74.         pop    dx
  75.         pop    cx
  76.         ret
  77. sl_atou2    endp
  78. ;
  79. ;
  80. ;
  81. ;
  82. NAtoI        proc    near
  83.         pushf
  84.         cld
  85. ;
  86. lp:        mov    al, es:[di]        ;Get byte at es:di
  87.         inc    di
  88.         xor    al, '0'
  89.         cmp    al, 10
  90.         ja    NotDigit
  91.         shl    cx, 1
  92.         jc    Error
  93.         mov    dx, cx
  94.         shl    cx, 1
  95.         jc    Error
  96.         shl    cx, 1
  97.         jc    Error
  98.         add    cx, dx
  99.         jc    Error
  100.         add    cl, al
  101.         adc    ch, 0
  102.         jc    Error
  103.         jmp    lp
  104. ;
  105. NotDigit:    dec    di        ;Because we inc'd after non-digit.
  106.         popf
  107.         clc
  108.         ret
  109. ;
  110. Error:        popf
  111.         stc
  112.         ret
  113. NAtoI        endp
  114. ;
  115. stdlib        ends
  116.         end
  117.